package com.cloud.springcloud.alibaba.controller;

import com.cloud.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName HuangXiangXiang
 * @Date 2020/4/5 17:47
 * @Version V1.0
 **/

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")  //全局的faback
public class OrderHystirxController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }


    // 80 hystrix


    /*
     @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
             @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
     })*/
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
    public String paymentInfo_TimeOut(Integer id) {
        int i = 1 / 0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }


    /*
     * 这个是使用全局的
     */
    @GetMapping("/consumer/payment/hystrix/timeout1/{id}")
    @HystrixCommand
    public String paymentInfo_TimeOut1(Integer id) {
        int i = 1 / 0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    /**
     * 这个是 特殊类的 需要单独设置 不用 全局的
     *
     * @param id
     * @return
     */

    @GetMapping("/consumer/payment/hystrix/timeout2/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String paymentInfo_TimeOut2(Integer id) {
        int i = 1 / 0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }


    //执行峰值时调用
    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id) {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }


    /**
     * 自定义全局fallback 方法
     */

    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后重试，😂";
    }

}
